﻿using MapleLeaf.Application.CookBook;
using MapleLeaf.Application.CookBook.Models;
using MapleLeaf.Core;
using MapleLeaf.Core.Dto;
using MapleLeaf.Model.CookBook;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace MapleLeaf.WebApi.CookBook.Controllers.CookBookManagement {
    /// <summary>
    /// 食谱管理
    /// </summary>
    public class RecipeController : ApiBaseController {
        private readonly CookBookManagementClient _managementClient;


        /// <summary>
        /// 
        /// </summary>
        public RecipeController(CookBookManagementClient managementClient) {
            _managementClient = managementClient;
        }


        #region 食谱
        /// <summary>
        /// 获取所有食谱
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PaginationModel<RecipeVo>>> GetRecipePageList(int pageNum, int pageSize) {
            var whereExp = Expressionable.Create<RecipeEntity>();
            PageModel pagination = new() {
                PageIndex = pageNum,
                PageSize = pageSize
            };
            var list = await _managementClient.GetRecipePageListAsync(whereExp.ToExpression(), pagination);
            var result = new PaginationModel<RecipeVo>() {
                Total = pagination.TotalCount,
                List = list.Adapt<List<RecipeVo>>()
            };
            return Success(result);
        }
        /// <summary>
        /// 获取所有食谱
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<RecipeVo>>> GetRecipeList() {
            var whereExp = Expressionable.Create<RecipeEntity>();
            //PaginationModel pagination = new() {
            //    PageIndex = pageNum,
            //    PageSize = pageSize
            //};
            var res = await _managementClient.GetRecipeListAsync(whereExp.ToExpression());
            var list = res.Adapt<List<RecipeVo>>();
            return Success(list);
        }
        /// <summary>
        /// 获取食谱
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<RecipeVo>> GetRecipeInfo(long id) {
            var result = await _managementClient.GetRecipeInfoAsync(id);
            var vo = result.Adapt<RecipeVo>();
            return Success(vo);
        }
        /// <summary>
        /// 保存食谱
        /// </summary>
        /// <param name="recipeVo"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> SaveRecipe(RecipeVo recipeVo) {
            var entity = recipeVo.Adapt<RecipeEntity>();
            var res = await _managementClient.SaveRecipeAsync(entity);

            return res ? Success() : Fail();
        }
        /// <summary>
        /// 删除食谱
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> DeleteRecipe(List<long> ids) {
            var res = await _managementClient.DeleteRecipeAsync(p => ids.Contains(p.Id));
            return res ? Success() : Fail();
        }
        #endregion

    }
}
